package com.newcoder.community.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @author edison
 * @version 2023
 * @date 2024/1/21 21:17
 */

public class CookieUtil {

    public static String getValue(HttpServletRequest request, String name) {

        // 判断参数是否为空,为空则抛出异常
        if (request == null || name == null) {
            throw new IllegalArgumentException("参数为空!");
        }

        // 获取cookies,遍历cookies,找到对应的cookie,返回cookie的值
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(name)) {
                    return cookie.getValue();
                }
            }
        }

        // 没有找到对应的cookie,返回null
        return null;
    }
}
